<?php

class Modules extends Onecore_Controller
{

    public function __construct()
    {
        parent::__construct();
        $this->load->model("modules_model");
        $this->load->model("objects_model");
        $this->load->model("groups_model");
        $this->_moduleName = utf8_encode("Modulo modulos_model");
        $this->_moduleDescription = utf8_encode("Description") . $this->_moduleName;
        $this->_modelInstance = $this->modules_model;
    }
    /*
     * permite validar si el modulo a cargar pertenece a algun grupo del usuario logueado
     */
    private function belongsToGroup($idModule){
        $groups = $this->session->userdata('groups');
        foreach ($groups as $group){
            if($this->auth_model->groupHasModule($group->groups_id,$idModule)){
                return true;
            }
        }
        return false;
    }
    /*
     * vista de lista e temas
     */
    public function index($id)
    {
        if(!$this->belongsToGroup($id)){
            header("HTTP/1.1 403 Forbidden . No tiene asignado el recurso solicitado");
            return;
        }
        //obtenemos modulo
        $modulo = $this->_modelInstance->getByid($id);
        if(is_array($modulo))   $modulo = $modulo[0];
        //obtenemos el grupo al que pertecene
        $group = $this->groups_model->getByid($modulo->groups_id);
        if(!empty($group[0]))   $group = $group[0];
        //buscamos objetos incrustados al grupo
        $objects = $this->objects_model->getByModule($modulo->id);
        
        $categoryObjects = array();
        foreach($objects as $object){
//            $categoryObjects[$object->category][] = array(
//                "name"=>$object->name,
//                "description"=>$object->description,
//                "id"=>$object->id,
//                "typeobject"=>$object->typeobject,
//                "categoryName"=>$object->category,
//            );
            $categoryObjects[$object->category][] = $object;
        }
        
        $data['user_id'] = $this->session->userdata('id_usuario');
        $data['categories'] = $categoryObjects;
        $data['breadcumb'] = $group->name." - ".$modulo->name;
        $banner["module_name"] = $modulo->name;
        $banner["module_description"] = $modulo->description;
        $data["module_banner"] = $this->load->view("/include/sections/module_banner/module_title",$banner,true);
        $data["page_title"] = $modulo->name;
        $this->layout->view("/modules/index", $data);
        
    }
    /*
     * vista de resumen de temas con detalle
     */
    public function summary($id)
    {
        if(!$this->belongsToGroup($id)){
            header("HTTP/1.1 403 Forbidden . No tiene asignado el recurso solicitado");
            return;
        }
        
        $modulo = $this->_modelInstance->getByid($id);
        if(is_array($modulo))   $modulo = $modulo[0];
        
        $group = $this->groups_model->getByid($modulo->groups_id);
        if(!empty($group[0]))   $group = $group[0];
        
        $objects = $this->objects_model->getByModule($modulo->id);
        
        $categoryObjects = array();
        foreach($objects as $object){
            $categoryObjects[$object->category][] = array(
                "name"=>$object->name,
                "description"=>$object->description,
                "id"=>$object->id,
                "typeobject"=>$object->typeobject,
                "categoryName"=>$object->category,
                "featuredImage"=>$object->featuredImage,
            );
        }
        
        
        $data['categories'] = $categoryObjects;
        $data['breadcumb'] = $group->name." - ".$modulo->name;
        $banner["module_name"] = $modulo->name;
        $banner["module_description"] = $modulo->description;
        $data["module_banner"] = $this->load->view("/include/sections/module_banner/module_title",$banner,true);
        $data["page_title"] = $modulo->name;
        $this->layout->view("/modules/summary", $data);
        
    }

}
